package com.Mms.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

@Controller
public class outputformat {
	static Logger log = Logger.getLogger(outputformat.class.getName());

	protected static void write(HttpServletResponse response, String str) {
		PrintWriter out = null;
		try {
			// 设置页面不缓存
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			response.setStatus(200);
			out = response.getWriter();
			out.print(str);
			out.flush();
		} catch (IOException e) {
			log.error(e.toString());
		} finally {
			out.close();
		}
	}

	protected static void writeJSON(HttpServletResponse response, String str) {
		PrintWriter out = null;
		try {
			// 设置页面不缓存
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			out = response.getWriter();
			out.print(str);
			out.flush();
		} catch (IOException e) {
			log.error(e.toString());
		} finally {
			out.close();
		}
	}

	protected static void writerError(HttpServletResponse response, String str) {
		PrintWriter out = null;
		try {
			// 设置页面不缓存
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/json");
			response.setHeader("error", str);
			response.setStatus(500);
			out = response.getWriter();
			out.print(str);
			out.flush();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			out.close();
		}
	}
	
	protected void writeAsFileStream(String filePath , HttpServletResponse response,Boolean needDeleteSrc){
		OutputStream os = null;
		InputStream is = null;
		try {
			os = response.getOutputStream();
		
			is = new FileInputStream(filePath);
			setFileOuputResponse(response, new File(filePath).getName());
			IOUtils.copy(is, os);
			response.flushBuffer();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} finally {
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}

			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
			if(needDeleteSrc) {
				new File(filePath).delete();
			}
		}
	}
	
	protected void setFileOuputResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
		response.setHeader("Content-Disposition", "attachment;fileName=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"");
	}
}

